package com.example.yan.excel_split.controller;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;


public class demo7 {

    @RequestMapping(value = "/q")
    public String aaa(){
        return "788888";
    }


    @RequestMapping(value = "/to",method = RequestMethod.GET)
    public void toDownload(HttpServletResponse response) {
        System.out.println("666");
        String fileName = "测试";

        try {
            List<Map<String, Object>> rows = new ArrayList<>();
            Random r = new Random();
            for (int i = 0; i < 10; i++) {

                Map<String, Object> map = new HashMap<>();
                map.put("ID", i + 1);
                map.put("名字", "的去" + i);
                map.put("年龄", i + 18);
                rows.add(map);
            }


            response.setContentType("application/octet-stream");
            //设置文件夹名称
            response.setHeader("Content-disposition", "attachment;fileName=" + fileName + ".xlsx");
            OutputStream out = response.getOutputStream();
            //用工具类创建writer  默认xls格式
            ExcelWriter writer = ExcelUtil.getWriter();
            writer.write(rows);
            writer.flush(out);
            writer.close();
            out.flush();
            System.out.println("执行完了");


        } catch (Exception e) {
            e.printStackTrace();
        }


    }

}
